<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>动态组件</title>
</head>
<body>
    <div id="app">
        <component :is="currentView"></component>
        <button @click="handleChangeView('A')">切换到A</button>
        <button @click="handleChangeView('B')">切换到B</button>
        <button @click="handleChangeView('C')">切换到C</button>
    </div>
    <div id="app-2">
        <component :is="currentView"></component>
    </div>
        <script src="https://cdn.jsdelivr.net/npm/vue@2.6.11"></script>
        <script>           
            var app = new Vue({
                components:{
                    comA:{
                        template:'<div>组件A</div>'
                    },
                    comB:{
                        template:'<div>组件B</div>'
                    },
                    comC:{
                        template:'<div>组件C</div>'
                    },
                },
                el:"#app",
                data:{
                    currentView:'comA'
                },
                methods:{
                    handleChangeView:function(components){
                        this.currentView = 'com' + components;
                    }
                }
            })
            var Home = {
                template:'<p>welcome home!</p>'
            };
            var app2 = new Vue({
                el:"#app-2",
                data:{
                    currentView:Home
                }
            });
        </script>
</body>
</html>